import { KEYBOARD_KEY } from '@app-cad/common/Constants';
import { wsApi } from '@app-cad/server';

import { BaseCommand, CENTEREVENTS, MouseStyle } from '@amcax/base';
import { Picker3D, PickerMgr } from '@amcax/renderer';
import { eventCenterManager } from '@app-base/common/events/EventCenterManager';
import { MessageTip } from '@app-cad/common/utils/MessageTip';
import { ctx2 } from '@app-cad/module';

// 拷贝
enum State {
  SELECT,
  MOVING,
  FINISH,
}

type MouseState = State.SELECT | State.MOVING | State.FINISH;
let inCopyCommand = false;
let currentStatus: MouseState = State.SELECT;
// let sceneSetup;
let dxid;

function init() {
  currentStatus = State.SELECT;
  // sceneSetup = ctx2.viewer.sceneSetup;
}

async function finish() {
  const transform = ctx2.dragController.getTransform();
  if (transform && dxid) {
    await wsApi.copy({
      o: dxid,
      l: `copy-${new Date().getTime()}`,
      transform: transform,
    });
  }
}

function enterMover() {
  const models = PickerMgr.getPicker<Picker3D>('3d').getSelectedModels();
  if (models.size > 0) {
    let pickModelData;
    for (const model of models) {
      dxid = model[0];
      pickModelData = model[1];
    }
    ctx2.dragController.start(pickModelData);
    currentStatus = State.MOVING;
  } else {
    MessageTip.warning('未选中物体');
  }
}

function onPointerDown() {}

function onPointerUp(e) {
  if (!e.button.onLeft) {
    return;
  }
  const picker = PickerMgr.getPicker<Picker3D>('3d');
  if (currentStatus === State.SELECT) {
    if (e.idsMap && e.idsMap.size > 0) {
      picker.selectModel(e.event);
    } else {
      picker.clickNull();
    }
  }
  // 处理框选
  picker.marqueePointerUp();
}

function onPointerMove(e) {
  if (currentStatus === State.SELECT) {
    const picker = PickerMgr.getPicker<Picker3D>('3d');
    picker.pick(e, { model: true });
    // 框选
    picker.marqueeSelection(e);
  }
}

function onKeyup(e) {
  // 进入mover
  if (e.key === KEYBOARD_KEY.SPACE || e.key === KEYBOARD_KEY.ENTER) {
    if (currentStatus === State.SELECT) {
      enterMover();
    } else if (currentStatus === State.MOVING) {
      finish();
      exitCommand();
      currentStatus = State.FINISH;
      MessageTip.success('复制完成');
    }
  }

  // 退出命令
  if (e.key === KEYBOARD_KEY.ESC) {
    exitCommand();
    MessageTip.info('退出复制命令');
  }
}

function enterCommand() {
  MessageTip.info('进入复制命令');
  init();
  MouseStyle.setImg('copy', 12, 12);
  eventCenterManager.activeEventCenter
    .setState('cmd-copy')
    .on(CENTEREVENTS.POINTERDOWN, onPointerDown)
    .on(CENTEREVENTS.POINTERUP, onPointerUp)
    .on(CENTEREVENTS.POINTERMOVE, onPointerMove)
    .on(CENTEREVENTS.KEYUP, onKeyup)
    .addExit(exitCommand);
  const picker = PickerMgr.getPicker<Picker3D>('3d');
  const models = picker.getSelectedModels();
  if (models.size > 0) {
    enterMover();
  }
}

function exitCommand() {
  inCopyCommand = false;
  const picker = PickerMgr.getPicker<Picker3D>('3d');
  picker.clickNull();
  picker.init();
  ctx2.dragController.distory();
  MouseStyle.setImg('');
  eventCenterManager.activeEventCenter
    .off(CENTEREVENTS.POINTERDOWN, onPointerDown)
    .off(CENTEREVENTS.POINTERUP, onPointerUp)
    .off(CENTEREVENTS.POINTERMOVE, onPointerMove)
    .off(CENTEREVENTS.KEYUP, onKeyup)
    .setDefault();
  ctx2.viewer.requestRender();
}

export const cmdCopy: BaseCommand = {
  id: 'CmdCopy',
  label: 'command.copy',
  icon: 'commands/feature/copy',
  // create: (label: string, onClose: () => void) => (
  //   <CopyCommand
  //     //title={i18n.t(label)}
  //     onClose={onClose}
  //   />
  // ),
  invoke: () => {
    if (!inCopyCommand) {
      enterCommand();
      inCopyCommand = true;
    }
  },
};
